﻿using System.Collections.Generic;
using System.Data;
using Ue.Infrastructure.RepositoryFramework;
using Ue.Qcdz.Domain.ShopArticle;
using Ue.Qcdz.Domain.Shops;

namespace Ue.Qcdz.Repositories.ShopArticle
{
    public class ShopNewsRepository : SqlServerRepository<ShopNews>, IShopNewsRepository
    {
        protected override System.Data.IDataParameter[] BuildParameters(ShopNews entity)
        {
            return new IDataParameter[]{
              this.RepositoryDriver.BuildParameter(ShopNewsFactory.FieldNames.EntityKey,entity.Key),
              this.RepositoryDriver.BuildParameter(ShopNewsFactory.FieldNames.Title,entity.Title??""),              
              this.RepositoryDriver.BuildParameter(ShopNewsFactory.FieldNames.Editor,entity.Editor??""),   
              this.RepositoryDriver.BuildParameter(ShopNewsFactory.FieldNames.Content,entity.Content??""),   
              this.RepositoryDriver.BuildParameter(ShopNewsFactory.FieldNames.Reference,entity.Reference??""),   
              this.RepositoryDriver.BuildParameter(ShopNewsFactory.FieldNames.Status,entity.Status),              
              this.RepositoryDriver.BuildParameter(ShopNewsFactory.FieldNames.Remark,entity.Remark??""),
              this.RepositoryDriver.BuildParameter(ShopNewsFactory.FieldNames.ShopId,entity.Shop.Key),
              this.RepositoryDriver.BuildParameter(ShopNewsFactory.FieldNames.DisplayOrder,entity.DisplayOrder),
              this.RepositoryDriver.BuildParameter(ShopNewsFactory.FieldNames.CreateOn,entity.CreateOn)
  
            };
        }

        protected override Infrastructure.RepositoryFramework.Repository.IWhereClause BuildWhereClauseBy(Infrastructure.DomainBase.IDataQuery dataQuery)
        {
            if (dataQuery is IShopNewsDataQuery)
            {
                return new ShopNewsDataQueryWhereClause(dataQuery as IShopNewsDataQuery);
            }

            return null;
        }

        protected override string[] GetBaseInsertFields()
        {
            return new string[] {
                    ShopNewsFactory.FieldNames.EntityKey,
                    ShopNewsFactory.FieldNames.Title,
                    ShopNewsFactory.FieldNames.Editor, 
                    ShopNewsFactory.FieldNames.Reference, 
                    ShopNewsFactory.FieldNames.Content,
                    ShopNewsFactory.FieldNames.ShopId,
                    ShopNewsFactory.FieldNames.Remark,
                    ShopNewsFactory.FieldNames.Status, 
                    ShopNewsFactory.FieldNames.DisplayOrder,
                    ShopNewsFactory.FieldNames.CreateOn
     
            };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[] {
                    ShopNewsFactory.FieldNames.Title,
                    ShopNewsFactory.FieldNames.Editor, 
                    ShopNewsFactory.FieldNames.Reference, 
                    ShopNewsFactory.FieldNames.Content,
                    ShopNewsFactory.FieldNames.Remark,
                    ShopNewsFactory.FieldNames.Status, 
                    ShopNewsFactory.FieldNames.DisplayOrder 
            };
        }

        protected override IDictionary<string, Infrastructure.RepositoryFramework.Repository.SqlServerRepositoryBase<ShopNews>.ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            var callBacks = new Dictionary<string, SqlServerRepository<ShopNews>.ChildDataHandler>();
            callBacks.Add(ShopNewsFactory.FieldNames.ShopId, delegate(ShopNews shopNews, object fieldValue)
            {
                ShopNewsFroxy shopNewsProxy = shopNews as ShopNewsFroxy;
                if (shopNewsProxy != null)
                {
                    IShopRepository shopRepository = RepositoryFactory.GetRepository<IShopRepository, Shop>();
                    shopNewsProxy.Shop = shopRepository[fieldValue];
                }
            });
            return callBacks;
        }

        protected override string GetDataTableName()
        {
            return "ShopNews";
        }

        #region IShopNewsRepository 成员

        public void RemoveAll(Shop shop)
        {
            if (shop == null) return;
            var query = "DELETE FROM ShopNews WHERE ShopId=@ShopId";
            var parameter = new IDataParameter[]
                                {
                                    this.RepositoryDriver.BuildParameter(ShopNewsFactory.FieldNames.ShopId,shop.Key),
                             
                                };
            this.BuildEntitiesFromSql(query, parameter);
        }

        #endregion
    }
}
